/**
 *
 */
package com.dhcc.android.base.utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.common.bean.BasicProperties;
import com.dhcc.android.base.common.bean.File;
import com.dhcc.android.base.common.bean.KeyValueBean;
import com.dhcc.android.base.common.view.ListViewPoupWindow;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.gis.FactoryGPS;
import com.dhcc.android.base.gis.SelectItemChangedListener;
import com.dhcc.android.base.gis.TouchLongToolCommen;
import com.example.collector.interoperation.CollectInteroperator;
import com.example.collector.interoperation.acticity.CollectActivity;
import com.example.collector.interoperation.acticity.ShearActivity;
import com.example.collector.interoperation.event.OnCollectBackListener;
import com.example.collector.interoperation.event.OnCollectSaveListener;
import com.example.collector.interoperation.event.OnEditBackListener;
import com.example.collector.interoperation.event.OnEditSaveListener;
import com.example.collector.interoperation.event.OnShearBackListener;
import com.example.collector.interoperation.event.OnShearSaveListener;
import com.example.collector.tools.SaveGeometry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import srs.GPS.GPSControl;
import srs.GPS.GPSConvert;
import srs.Geometry.Envelope;
import srs.Geometry.FormatConvert;
import srs.Geometry.IEnvelope;
import srs.Geometry.IGeometry;
import srs.Geometry.IPoint;
import srs.Geometry.Point;
import srs.Rendering.CommonRenderer;
import srs.tools.MapControl;
import srs.tools.ZoomInCommand;
import srs.tools.ZoomOutCommand;

/**
 * 图斑地块处理包
 *
 * @author pengling
 * @date 2015-7-10
 */
public class DemandDKMapUtil {

    private Activity activity;
    private final CharSequence[] items = {"Point", "Line", "Polygon"};
    private ListViewPoupWindow listViewPoupWindow;
    private MapControl demandMapControl;

    public DemandDKMapUtil(Activity activity) {
        this.activity = activity;
    }

    /**
     * 初始化新建地块，待完善
     *
     */
    @SuppressWarnings("unused")
    public void initDKMapData(BasicProperties maps, View layout, final File file,final DemandDKMapListener demandDKMapListener) {
        if (maps.getType().equals(Constants.SELF)) {
            View childView = (View) activity.getLayoutInflater().inflate(
                    R.layout.util_dk_new, null);
            OnClickListener addClick = new OnClickListener() {// 新建单位区属性
                @Override
                public void onClick(View v) {
                    if (StringUtil.isFastDoubleClick(500)) {
                        return;
                    }
                    addDK(file);
                }
            };
            OnClickListener editClick = new OnClickListener() {// 编辑单位区属性
                @Override
                public void onClick(View v) {
                    if (StringUtil.isFastDoubleClick(500)) {
                        return;
                    }
                    editDK(file);
                }
            };
            OnClickListener clipClick = new OnClickListener() {// 分割单位区属性
                @Override
                public void onClick(View v) {
                    if (StringUtil.isFastDoubleClick(500)) {
                        return;
                    }
                    clipDK(file);

                }
            };
            OnClickListener deleteClick = new OnClickListener() {// 分割单位区属性
                @Override
                public void onClick(View v) {
                    if (StringUtil.isFastDoubleClick(500)) {
                        return;
                    }
                    delDK(file, Constants.surveyDKIDPath,Constants.surveyDKIDPathNoData);

                }
            };

            Map<Integer, OnClickListener> imgMap = new HashMap<Integer, OnClickListener>();
            List<KeyValueBean> keyValueList = new ArrayList<KeyValueBean>();
            List<BasicProperties> actionList = new ArrayList<BasicProperties>();
            actionList = file.getMaps().getAction();
            for (BasicProperties action : actionList) {
                String title = action.getTitle();
                if (!action.isShow()) {
                    keyValueList.add(new KeyValueBean("", title));
                } else {
                    if (title.equals("剪切")) {// 剪切监听
                        imgMap.put(R.mipmap.split_white, clipClick);
                    } else if (title.equals("编辑")) {
                        imgMap.put(R.mipmap.edit_white, editClick);
                    } else if (title.equals("删除")) {// 为实现
                        imgMap.put(R.mipmap.delete_white, deleteClick);
                    } else if (title.equals("新增")) {
                        imgMap.put(R.mipmap.add_white, addClick);
                    }
                }
            }
            initDK(childView, layout, keyValueList, file);
            demandDKMapListener.setImageBtnClick(layout, imgMap);
        }
    }

    /**
     * 新建地块
     *
     * @param file
     */
    public void addDK(final File file) {
        // add 杨宗仁 空间编辑，新建页面的跳转
        // CommenUtil.getMap()从当前mapControl获取map
        // srsGeometryType.Polygon是编辑多边形的类型参数，类型参数分为点、线、面
        System.out.println("!!:" + Constants.geometrys);
        CollectInteroperator.init(CommenUtil.getMap(), CommenUtil.getGeo(0)
                .GeometryType());
        CollectInteroperator.CollectEventManager
                .setOnCollectSaveListener(new SaveGeoListener(file));
        CollectInteroperator.CollectEventManager
                .setOnCollectBackListener(new BackGeoListener());
        Intent intent = new Intent(activity, CollectActivity.class);
        activity.startActivity(intent);
    }

    /**
     * 删除地块
     */
    private void delDK(final File file, String surveyDKIDPath, String surveyDKIDPathNoData) {
        boolean isCopy = copyFileByisExist();
        int rowId = DBManagerUtil.getRowId(
                TouchLongToolCommen.currentIndex + 1 - 1, file.getTable(), surveyDKIDPath);
        int count = DBManagerUtil.getCount(surveyDKIDPath, file.getTable(), null, null);
        System.out.println("记录数：" + count);
        if (count <= 1) {
            CommenUtil.toast("当前地块数据无法删除!", activity);
        } else {
            boolean isSuccess = DBManagerUtil.deleteById(
                    file.getTable(), "rowid", rowId /*
                                                     * TouchLongToolCommen.
													 * currentIndex +1
													 */,
                    surveyDKIDPath);
            if (isSuccess) {
                if (Constants.geometrys.size() > 0) {
                    Constants.geometrys
                            .remove(TouchLongToolCommen.currentIndex);
                }
                Constants.COMMONLAYER.setInitInfos("TableName",
                        Constants.geometrys, null,
                        srs.Geometry.srsGeometryType.Polygon,
                        Constants.displayLableNames, new Envelope(), null);
                ((CommonRenderer) Constants.COMMONLAYER.getRenderer())
                        .setSymbol(Constants.SYMBOLCBJ);

                demandMapControl.Refresh();
                CommenUtil.toast("删除成功！", activity);
            } else {
                /**
                 * add by pengling;增加删除成功判断
                 */
                if (isCopy) {// 新复制
                    FileUtil.deleteFile(surveyDKIDPathNoData);
                }
                CommenUtil.toast("删除失败，请重试！", activity);
            }
        }

    }

    /**
     * 剪切地块
     *
     * @param file
     */
    public void clipDK(File file) {
        // add 杨宗仁 空间编辑,剪裁页面的跳转
        // CommenUtil.getMap()从当前mapControl获取map
        // new ArrayList<IGeometry>()剪切的几何对象的集合
        // 剪裁可保存为WKT格式

        // 获取要剪切的目标
        List<IGeometry> geosCurrent = new ArrayList<IGeometry>();
        geosCurrent.add(CommenUtil.getGeo(TouchLongToolCommen.currentIndex));

        CollectInteroperator.init(CommenUtil.getMap(), geosCurrent);
        CollectInteroperator.CollectEventManager
                .setOnShearSaveListener(new SaveGeoListener(file));
        CollectInteroperator.CollectEventManager
                .setOnShearBackListener(new BackGeoListener());
        Intent intent = new Intent(activity, ShearActivity.class);
        activity.startActivity(intent);
    }

    /**
     * 编辑
     *
     * @param file
     */
    private void editDK(File file) {

        CollectInteroperator.init(CommenUtil.getMap(),
                CommenUtil.getGeo(TouchLongToolCommen.currentIndex));
        CollectInteroperator.CollectEventManager
                .setOnEditSaveListener(new SaveGeoListener(file));
        CollectInteroperator.CollectEventManager
                .setOnEditBackListener(new BackGeoListener());
        Intent intent = new Intent(activity, CollectActivity.class);
        activity.startActivity(intent);
    }

    /**
     * 空间编辑保存接口
     * <p/>
     * 杨宗仁
     *
     * @author Tudou
     */
    public class SaveGeoListener implements OnCollectSaveListener, OnShearSaveListener, OnEditSaveListener {
        private File file;

        public SaveGeoListener(File file) {
            this.file = file;
        }

        @Override
        public boolean collectSave(EventObject event, double area) {// 新建保存
            String wkt = "";
            boolean result = true;
            try {
                wkt = SaveGeometry.saveAsWKT();
                // 数据库中的GEO转换成wkt函数
                if (StringUtil.isNotEmpty(wkt)) {
                    IGeometry geo = FormatConvert.WKTToPolygon(wkt);
                    Constants.geometrys.add(geo);

                    result = doWithGeo(file, wkt);
                }
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
            }

            return result;
        }

        @Override
        public boolean shearSave(EventObject event, double[] areas) {// 剪切保存
            List<String> wktList = null;
            try {
                wktList = SaveGeometry.saveShearedGeosAsWKT();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            boolean isCopy = copyFileByisExist();
            boolean isSuccess = Constants.dbManagerUtil.deleteById(
                    file.getTable(), "rowid",
                    TouchLongToolCommen.currentIndex + 1,Constants.surveyDKIDPath);
            if (isSuccess) {
                if (Constants.geometrys.size() > 0) {
                    Constants.geometrys
                            .remove(TouchLongToolCommen.currentIndex);
                }
            } else {
                if (isCopy) {// 新复制
                    FileUtil.deleteFile(Constants.surveyDKIDPathNoData);
                }
            }
            for (String wkt : wktList) {
                if (!StringUtil.isEmpty(wkt)) {
                    doWithGeoForShear(file, wkt);
                }
            }

            Constants.COMMONLAYER.setInitInfos("TableName",
                    Constants.geometrys, null, srs.Geometry.srsGeometryType.Polygon,
                    Constants.displayLableNames, new Envelope(), null);
            ((CommonRenderer) Constants.COMMONLAYER.getRenderer())
                    .setSymbol(Constants.SYMBOLCBJ);
            demandMapControl.Refresh();
            // 几何要素现实的图层
            return true;
        }

        @Override
        public boolean editSave(EventObject event) {// 编辑保存
            String wkt = "";
            try {
                wkt = SaveGeometry.saveAsWKT();
                IGeometry geo = FormatConvert.WKTToPolygon(wkt);
                // Geometry geo=Geometry.CreateFromWkt(wkt);
                // 删除选中的编辑前的形状
                Constants.geometrys.remove(TouchLongToolCommen.currentIndex);
                Constants.geometrys.add(TouchLongToolCommen.currentIndex, geo);

            } catch (IOException e) {
                e.printStackTrace();
            }

            return doWithGeo(file, wkt);
        }
    }

    private Boolean doWithGeo(File file, String wkt) {
        Boolean isSuccess = false;
        try {
            Map<String, String> saveMap = new HashMap<String, String>();
            // 暂存DKID、GEO两个参数
            saveMap.put(file.getPfeild(), Constants.DKID);
            saveMap.put(Constants.GEO, wkt);
            int size = Constants.dbManagerUtil.getCount(
                    Constants.surveyDKIDPath, file.getTable(),
                    file.getPfeild(), Constants.DKID);
            List<String> keyList = new ArrayList<String>();
            keyList.add(file.getPfeild());
            keyList.add(Constants.GEO);
            boolean isCopy = copyFileByisExist();
            if (size != 0) {// 修改
                // 由于数据库中获取指定N行的记录用limit n-1,1,故这里先-1
                int rowId = Constants.dbManagerUtil.getRowId(
                        TouchLongToolCommen.currentIndex + 1 - 1,
                        file.getTable(), Constants.surveyDKIDPath);
                isSuccess = Constants.dbManagerUtil.updateById(saveMap, keyList, file.getTable(), "rowid", rowId + "",Constants.surveyDKIDPath);// 只有一行数据

            } else {
                // 添加
                isSuccess = Constants.dbManagerUtil.add(saveMap,
                        keyList, file.getTable(), file.getPfeild(),
                        Constants.surveyDKIDPath);// 只有一行数据
            }

            Constants.COMMONLAYER.setInitInfos("TableName",
                    Constants.geometrys, null, srs.Geometry.srsGeometryType.Polygon,
                    Constants.displayLableNames, new Envelope(), null);
            ((CommonRenderer) Constants.COMMONLAYER.getRenderer())
                    .setSymbol(Constants.SYMBOLCBJ);

            demandMapControl.Refresh();

            // 几何要素现实的图层
            if (!isSuccess) {
                if (isCopy) {// 新复制
                    FileUtil.deleteFile(Constants.surveyDKIDPathNoData);
                }
                CommenUtil.toast("保存失败！", activity);
            }
            // Toast.makeText(activity, wkt, 1000).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    /**
     * 切割数据库存储操作
     *
     * @param file
     * @param wkt
     * @return
     */
    private Boolean doWithGeoForShear(File file, String wkt) {
        Boolean isSuccess = false;
        try {
            Map<String, String> saveMap = new HashMap<String, String>();
            // 暂存DKID、GEO两个参数
            saveMap.put(file.getPfeild(), Constants.DKID);
            saveMap.put(Constants.GEO, wkt);
            List<String> keyList = new ArrayList<String>();
            keyList.add(file.getPfeild());
            keyList.add(Constants.GEO);
            boolean isCopy = copyFileByisExist();
            isSuccess = Constants.dbManagerUtil.add(saveMap, keyList,
                    file.getTable(), file.getPfeild(),
                    Constants.surveyDKIDPath);// 只有一行数据
            Constants.geometrys.add(FormatConvert.WKTToPolygon(wkt));

            if (!isSuccess) {
                if (isCopy) {// 新复制
                    FileUtil.deleteFile(Constants.surveyDKIDPathNoData);
                }
                CommenUtil.toast("保存失败！", activity);
            }
            // Toast.makeText(activity, wkt, 1000).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    /**
     * 空间编辑返回接口
     * <p/>
     * 杨宗仁
     *
     * @author Tudou
     */
    public class BackGeoListener implements OnCollectBackListener,
            OnShearBackListener, OnEditBackListener {

        @Override
        public boolean collectBack(EventObject event) {
            System.out.println("点击新建返回");
            return true;
        }

        @Override
        public boolean shearBack(EventObject event) {
            return true;
        }

        @Override
        public boolean editBack(EventObject event) {
            return true;
        }
    }

    /**
     * 刷新tab 的地图数据
     *
     * @param childView
     */
    /**
     * @param childView
     * @param file
     */
    public void refreshCheckTab(View childView, File file) {

		/*
         * added by 李忠义 20150814 去掉前一个页面中在CommonLayer里的选中状态
		 */
        try {
            CommenUtil.getMap().getElementContainer().ClearElement();
        } catch (IOException e) {
            e.printStackTrace();
        }

		/* 获取到要显示的几何要素 */
        // 全局变量
        // 从数据库中获取数据
        List<String> strList = new ArrayList<String>();
        strList.add("GEO");
        if (StringUtil.isNotEmpty(file.getDklabelname())) {
            strList.add(file.getDklabelname());
        }
        System.out.println("-----strList:" + strList);
        final List<Map<String, String>> entityList = Constants.dbManagerUtil
                .getListData(file.getTable(), strList, file.getPfeild(),
                        Constants.DKID,
                        Constants.surveyDKIDPath);

		/*
         * added by lizhongyi 20150809 标签一定是要清空的
		 */
        Constants.displayLableNames.clear();
        if (entityList != null && entityList.size() != 0) {// 查出为[{GEO=null},{GEO=null}]
            boolean isFirst = true;
            try {
                for (Map<String, String> map : entityList) {
                    String wkt = map.get("GEO");

                    if (StringUtil.isNotEmpty(wkt)) {
                        System.out.println("-----------" + wkt);
                        IGeometry geo = FormatConvert.WKTToPolygon(wkt);
                        if (geo != null) {
                            if (isFirst) {// 有数据后 第一次进来的时候 将清空
                                isFirst = false;
                                Constants.geometrys.clear();
                            }
                            Constants.geometrys.add(geo);
                        }

                    }

					/*
                     * edited by lizhongyi 20150809 String LableID =
					 * map.get("DKNUMBER"); label未必都是DKNUMBER,应该是配置文件中取出来的
					 */
                    String LableID = "";
                    if (StringUtil.isNotEmpty(file.getDklabelname())) {
                        LableID = map.get(file.getDklabelname());
                    }

					/* edited by lizongyi 20150809 */
                    /* if (isNotEmpty(LableID)) { */
                    if (LableID != null) {
                        System.out.println("-----------" + LableID);
                        /* if (LableID != null) { */
                        /*
                         * 就不会走到这里，因为前面geo里已经把isFirst设为false了 if (isFirst) {//
						 * 有数据后 第一次进来的时候 将清空 isFirst = false;
						 * Constants.displayLableNames.clear(); }
						 */
                        Constants.displayLableNames.add(LableID);
                        /* } */

                    } else {
                        Constants.displayLableNames.add("");
                        System.out.println("未填写标注" + LableID);
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("~~~~:" + Constants.geometrys);
        // 几何要素现实的图层
        Constants.COMMONLAYER.setInitInfos("TableName"/* file.getTable() */,
                Constants.geometrys, null, srs.Geometry.srsGeometryType.Polygon,
                Constants.displayLableNames, new Envelope(), null);/* 不同页面更换数据与表名 */
        ((CommonRenderer) Constants.COMMONLAYER.getRenderer())
                .setSymbol(Constants.SYMBOLCBJ);
    }

    ;

    /**
     * 初始化大地图
     *
     * @param childView    有地图的View
     * @param parentlayout 放地图的父窗体
     */
    public void initDK(View childView, final View parentlayout,
                       final List<KeyValueBean> keyValueList, final File file) {
        TextView title_textview = (TextView) childView
                .findViewById(R.id.title_textview);
        title_textview.setText(file.getMaps().getTitle());
        IEnvelope env = CommenUtil.getMap().getExtent();
        demandMapControl = (MapControl) childView.findViewById(R.id.mapmain);
        demandMapControl.getActiveView().FocusMap().setExtent(env);

        demandMapControl.getActiveView().FocusMap(CommenUtil.getMap());

        demandMapControl.getActiveView().FocusMap().setExtent(env);

        CommenUtil.getMap().setDeviceExtent(
                new Envelope(0, 0, demandMapControl.getWidth(),
                        demandMapControl.getHeight()));

        // 刷新图层
        demandMapControl.Refresh();

        ImageButton btnZoomin = (ImageButton) childView
                .findViewById(R.id.zoom_in);
        ImageButton btnZoomout = (ImageButton) childView
                .findViewById(R.id.zoom_out);
        ImageView ivLocationCenter = (ImageView) childView
                .findViewById(R.id.iv_location_center);
        if (btnZoomin != null) {
            btnZoomin.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            ZoomInCommand zoomin = new ZoomInCommand();
                            zoomin.setBuddyControl(demandMapControl);
                            zoomin.setEnable(true);
                            zoomin.onClick(v);
                    }
                    return false;
                }
            });
        }
        if (btnZoomout != null) {
            btnZoomout.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            ZoomOutCommand zoomout = new ZoomOutCommand();
                            zoomout.setBuddyControl(demandMapControl);
                            zoomout.setEnable(true);
                            zoomout.onClick(v);
                    }
                    return false;
                }
            });
        }

        if (ivLocationCenter != null) {
            ivLocationCenter.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    SetLocationCenter(demandMapControl);
                    return false;
                }
            });
        }
        LinearLayout mapl = (LinearLayout) parentlayout
                .findViewById(R.id.map_layout);
        mapl.setVisibility(View.VISIBLE);

        // add 杨宗仁 GPS信号显示
        TextView tv_info = (TextView) childView.findViewById(R.id.info);
        tv_info.setBackgroundColor(Color.argb(128, 255, 255, 255));
        tv_info.setTextColor(Color.BLACK);
        try {
            FactoryGPS factory = new FactoryGPS(tv_info, null, null, null,
                    demandMapControl);
            FactoryGPS.NaviStart = false;
            factory.StartStopGPS(activity, tv_info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mapl.addView(childView);
        demandMapControl.Refresh();

		/* 获取到要显示的几何要素 */
        // 全局变量
        // 从数据库中获取数据
        List<String> strList = new ArrayList<String>();
        strList.add("GEO");

        final List<Map<String, String>> entityList = Constants.dbManagerUtil
                .getListData(file.getTable(), strList, file.getPfeild(),
                        Constants.DKID,
                        Constants.surveyDKIDPath);
        if (entityList.size() != 0) {// 查出为[{GEO=null},{GEO=null}]
            boolean isFirst = true;
            try {
                for (Map<String, String> map : entityList) {
                    String wkt = map.get("GEO");

                    if (StringUtil.isNotEmpty(wkt)) {
                        System.out.println("-----------" + wkt);
                        IGeometry geo = FormatConvert.WKTToPolygon(wkt);
                        if (geo != null) {
                            if (isFirst) {// 有数据后 第一次进来的时候 将清空
                                isFirst = false;
                                Constants.geometrys.clear();
                            }
                            Constants.geometrys.add(geo);
                        }

                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("~~~~:" + Constants.geometrys);
        // 几何要素现实的图层
        Constants.COMMONLAYER.setInitInfos("TableName"/* file.getTable() */,
                Constants.geometrys, null, srs.Geometry.srsGeometryType.Polygon,
                Constants.displayLableNames, new Envelope(), null);/* 不同页面更换数据与表名 */
        ((CommonRenderer) Constants.COMMONLAYER.getRenderer())
                .setSymbol(Constants.SYMBOLCBJ);

		/* CommenLayer的点击事件 */
        TouchLongToolCommen touchLongTool = new TouchLongToolCommen(
                Constants.COMMONLAYER);
        demandMapControl.ClearDrawTool();
        touchLongTool.setBuddyControl(demandMapControl);// mapControl为操作的地图控件
        touchLongTool.onClick(demandMapControl);
        touchLongTool.setEnable(true);
        // 设置不累计选择项目
        touchLongTool.IsOnlyOneTime = false;
        // 设置是不为单选
        touchLongTool.IsOnlyOneSelect = false;
        // 设置选中或未选中条目时，触发的事件
        touchLongTool.zoom2Selected = new SelectItemChangedListener() {

            @Override
            public void doEventSettingsChanged(Object tar) {
                Integer index = (Integer) tar;
                System.out.println("####点选的编辑/分割的小地块顺序号：" + index);
                /* 选中后要触发的事件 */
                showOperateListPoup(parentlayout, keyValueList, file);
            }
        };
        demandMapControl.setDrawTool(touchLongTool);
    }

    private static MapControl clickDemandMapControl;// 当前点击的图层（设为静态保险，可能在外面转一圈值会变没）

    private void showOperateListPoup(View v,
                                     final List<KeyValueBean> keyValueList, final File file) {

        listViewPoupWindow = new ListViewPoupWindow(activity, "请选择相应操作：");
        listViewPoupWindow.initPopWindow(v, keyValueList, false);
        listViewPoupWindow.listView
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                        if (StringUtil.isFastDoubleClick(500)) {
                            return;
                        }
                        listViewPoupWindow.dismiss();
                        KeyValueBean keyValue = keyValueList.get(arg2);
                        String value = keyValue.getValue();
                        if (value.equals("剪切")) {// 剪切监听
                            clipDK(file);
                        } else if (value.equals("编辑")) {
                            editDK(file);
                        } else if (value.equals("新增")) {
                            addDK(file);
                        } else if (value.equals("删除")) {
                            delDK(file, Constants.surveyDKIDPath,Constants.surveyDKIDPathNoData);
                        } else if (value.equals("属性信息")) {
                            showDKInfo(file);
                        } else {
                            CommenUtil.toast("没有对应的操作...", activity);
                        }

                    }
                });
    }

    private void showDKInfo(File file) {
        int rowId = Constants.dbManagerUtil.getRowId(
                TouchLongToolCommen.currentIndex + 1 - 1, file.getTable(),
                Constants.surveyDKIDPath);
        System.out.println("rowid:" + rowId);
        String rowId_str = "";
        if (rowId != 0) {
            rowId_str = rowId + "";
        }
//        Intent intent = new Intent(activity, AddFormActivity.class);
//        intent.putExtra("rowid", rowId_str);
//        intent.putExtra("path", Constants.path);
//        intent.putExtra("surveyDKIDPath",Constants.surveyDKIDPath);
//        intent.putExtra("file", file);
//        intent.putExtra("bp", file.getMaps());
//        intent.putExtra(file.getPfeild(), Constants.DKID);
//        activity.startActivity(intent);
    }

    /**
     * 设置当前位置屏幕局中
     */
    public static void SetLocationCenter(MapControl mapControl) {
        IEnvelope mapEnv = mapControl.getActiveView().FocusMap().getExtent();
        GPSControl gpsControl = GPSControl.getInstance();
        double xy[] = GPSConvert.GEO2PROJECT(gpsControl.GPSLongitude,
                gpsControl.GPSLatitude, mapControl.getActiveView().FocusMap()
                        .getGeoProjectType());
        IPoint centerPoint = new Point(xy[0], xy[1]);
        double width = mapEnv.XMax() - mapEnv.XMin();
        double height = mapEnv.YMax() - mapEnv.YMin();
        IEnvelope envelope = new Envelope(centerPoint.X() - width / 2,
                centerPoint.Y() - height / 2, centerPoint.X() + width / 2,
                centerPoint.Y() + height / 2);
        mapControl.getActiveView().FocusMap().setExtent(envelope);
        mapControl.Refresh();
    }


    public interface DemandDKMapListener {

        void setImageBtnClick(View layout, Map<Integer, OnClickListener> imgMap);
    }
    /**
     * 判断PHOTOS.db是否存在，若非，则复制
     */
    public boolean copyFileByisExist() {
        if (!FileUtil.isExistFile(Constants.surveyDKIDPath)) {// 新文件地址不存在
            FileUtil.copyFile(Constants.path + "DATA.db", Constants.surveyDKIDPath);
            return true;
        }
        return false;
    }
}
